package com.gec.controller;

import com.gec.domain.Dept;
import com.gec.domain.Role;
import com.gec.domain.User;
import com.gec.exception.AuthenticateException;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class DB {

    private final Map<String, User> data = new HashMap<>();

    {
        Set<Role> R1 = new HashSet<>();
        Set<Role> R2 = new HashSet<>();
        Set<Role> R3 = new HashSet<>();
        R1.add(new Role("r01", "boss"));
        R2.add(new Role("r02", "leader"));
        R3.add(new Role("r05", "employee"));

        Dept D1 = new Dept("d01", "总经办");
        Dept D5 = new Dept("d05", "开发部");
        Dept D6 = new Dept("d06", "业务部");

        //u01(职员)[D5]
        User u01 = makeUser("u01", "andy", "安迪",
                D5, R3);
        //u02(主管)[D5]
        User u02 = makeUser("u02", "candy", "肯迪",
                D5, R2);
        //u03(boss)[D1]
        User u03 = makeUser("u03", "boss", "老板",
                D1, R1);
        //u06(主管)[D6]
        User u06 = makeUser("u06", "kailen", "凯伦",
                D6, R2);
        data.put("andy", u01);
        data.put("candy", u02);
        data.put("boss", u03);
        data.put("kailen", u06);
    }

    User makeUser(String id, String account,
                  String nickName, Dept d,
                  Set<Role> roles) {
        User user = new User();
        user.setId(id);
        user.setAccount(account);
        user.setNickName(nickName);
        user.setDept(d);
        user.setRoles(roles);
        return user;
    }

    public User getUser(String account)
            throws AuthenticateException {
        User user = data.get(account);
        if (user == null) {
            throw new AuthenticateException(
                    "用户不存在异常.");
        }
        return user;
    }

    public static void main(String[] args)
            throws Exception {
        DB db = new DB();
        User user = db.getUser("boss");
        System.out.println(user);
        System.out.println(user.getPrimaryRole());
        System.out.println(user.getDept());
    }


}

